package com.edge.pbx.view.summing;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.edge.pbx.view.PbxCircleSeekBar;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxRmoeteDelayLayout extends ViewGroup {
    public PbxRmoeteDelayLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        View view = getChildAt(0);
        if (view != null){
            final int width = MeasureSpec.getSize(widthMeasureSpec);
            final int height = MeasureSpec.getSize(heightMeasureSpec);

            final int childSize = (int) Math.max(width*0.35f,height*0.45f);
//            final int baseChildSize = 360;
            final int spec = MeasureSpec.makeMeasureSpec(childSize,MeasureSpec.EXACTLY);
            YzDebug.d("Yang", "---childSize:"+childSize);
            view.measure(spec, spec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View view = getChildAt(0);
        if (view != null){
            final int w = r - l;
            final int h = b - t;
            l = (w - view.getMeasuredWidth())/2;
            t = (h - view.getMeasuredHeight())/2;
            YzDebug.d("Yang","layout  "+ l + " " + t +" " +view.getMeasuredWidth());
            view.layout(l, t, l+view.getMeasuredWidth(), t + view.getMeasuredHeight());
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        final String fmt = "%.01f S";
        PbxCircleSeekBar bar = findViewById(R.id.pbx_summing_remote_delay_seekbar);
        if (bar != null){
            bar.setOnCircleSeekBarTextListener(value->String.format(fmt, value/10f));
        }
    }
}
